package com.yjd.pub.project.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.base.dao.IIntroDetailDao;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.project.model.ProjectCaseModel;
import com.yjd.comm.project.service.IProjectCaseService;
import com.yjd.pub.project.dao.IProjectCaseDao;

/**
 * <b>description</b>：项目案例业务实现<br>
 * <b>time</b>：2017-06-27 08:55:25 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("projectCaseService")
public class ProjectCaseServiceImpl extends BaseServiceImpl implements IProjectCaseService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public ProjectCaseModel insert(ProjectCaseModel model) throws Exception {
		return this.projectCaseDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(ProjectCaseModel model) throws Exception {
		return this.projectCaseDao.updateModel(model, true);
	}

	@Override
	public int createOrUpdate(ProjectCaseModel pcm, String caseDetail) throws Exception {

		Long currentTime = FrameUtil.getTime();
		// 创建详情
		IntroDetailModel detail = new IntroDetailModel();
		detail.setContent(caseDetail);
		detail.setRef_type(ReferenceTypeEnum.PROJECTCASE.getCode());
		detail.setLast_update_time(currentTime);
		if(null == pcm.getDetail_id()){
			detail.setCreate_time(currentTime);
			detail = introDetailDao.insertModel(detail, true);
		}else{
			detail.setDetail_id(pcm.getDetail_id());
			introDetailDao.updateModel(detail, true);
		}

		pcm.setDetail_id(detail.getDetail_id());
		pcm.setLast_update_time(currentTime);
		if(null == pcm.getCase_id()){
			pcm.setCreate_time(currentTime);
			pcm = projectCaseDao.insertModel(pcm, true);
		}else{
			projectCaseDao.updateModel(pcm, true);
		}

		return 1;
	}

	/**
	 * 根据id删除
	 * 
	 * @param case_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long case_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("case_id", case_id);
		return this.projectCaseDao.deleteModel(paramMap, true);
	}

	@Resource
	private IIntroDetailDao introDetailDao;

	@Resource
	private IProjectCaseDao projectCaseDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.projectCaseDao;
	}

}
